accel label: Fix RTL handling
authorMatthias Clasen <mclasen@redhat.com>
Sun, 17 Jan 2016 19:08:22 +0000 (14:08 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 17 Jan 2016 19:08:22 +0000 (14:08 -0500)
The 'mad hack' that GtkAccelLabel used to affect the GtkLabel
draw function broke with the introduction of gadgets, since
the positioning is no longer relative to the widgets' allocation
at the time of the call, but rather to the gadgets allocation.

Instead of coming up with an even madder hack to keep this
working, give the GtkLabel draw function knowledge about accel
labels.

https://bugzilla.gnome.org/show_bug.cgi?id=760663

gtk/gtkaccellabel.c
gtk/gtklabel.c

index 35938293e294500325a1488f7999a40a3b30f414..e9d88cb6e338431300255003878e630a9f9d8483 100644 (file)
  * accelerator key on the right of the label text, e.g. “Ctl+S”.
  * It is commonly used in menus to show the keyboard short-cuts for commands.
  *
- * The accelerator key to display is not set explicitly.
- * Instead, the #GtkAccelLabel displays the accelerators which have been added to
- * a particular widget. This widget is set by calling
- * gtk_accel_label_set_accel_widget().
+ * The accelerator key to display is typically not set explicitly (although it
+ * can be, with gtk_accel_label_set_accel()). Instead, the #GtkAccelLabel displays
+ * the accelerators which have been added to a particular widget. This widget is
+ * set by calling gtk_accel_label_set_accel_widget().
  *
- * For example, a #GtkMenuItem widget may have an accelerator added to emit the
- * “activate” signal when the “Ctl+S” key combination is pressed.
+ * For example, a #GtkMenuItem widget may have an accelerator added to emit
+ * the “activate” signal when the “Ctl+S” key combination is pressed.
  * A #GtkAccelLabel is created and added to the #GtkMenuItem, and
  * gtk_accel_label_set_accel_widget() is called with the #GtkMenuItem as the
  * second argument. The #GtkAccelLabel will now display “Ctl+S” after its label.
@@ -449,12 +449,12 @@ gtk_accel_label_draw (GtkWidget *widget,
                       cairo_t   *cr)
 {
   GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
-  GtkTextDirection direction;
   guint ac_width;
   GtkAllocation allocation;
   GtkRequisition requisition;
 
-  direction = gtk_widget_get_direction (widget);
+  GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget, cr);
+
   ac_width = gtk_accel_label_get_accel_width (accel_label);
   gtk_widget_get_allocation (widget, &allocation);
   gtk_widget_get_preferred_size (widget, NULL, &requisition);
@@ -465,45 +465,21 @@ gtk_accel_label_draw (GtkWidget *widget,
       PangoLayout *label_layout;
       PangoLayout *accel_layout;
       GtkLabel *label = GTK_LABEL (widget);
-
       gint x;
       gint y;
 
       context = gtk_widget_get_style_context (widget);
-      label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
 
-      cairo_save (cr);
+      label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
+      accel_layout = gtk_accel_label_get_accel_layout (accel_label);
 
-      /* XXX: Mad hack: We modify the label's width so it renders
-       * properly in its draw function that we chain to.
-       */
-      if (direction == GTK_TEXT_DIR_RTL)
-        cairo_translate (cr, ac_width, 0);
-      if (gtk_label_get_ellipsize (label))
-        pango_layout_set_width (label_layout,
-                                pango_layout_get_width (label_layout) 
-                                - ac_width * PANGO_SCALE);
-      
-      allocation.width -= ac_width;
-      gtk_widget_set_allocation (widget, &allocation);
-      GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget, cr);
-      allocation.width += ac_width;
-      gtk_widget_set_allocation (widget, &allocation);
-      if (gtk_label_get_ellipsize (label))
-        pango_layout_set_width (label_layout,
-                                pango_layout_get_width (label_layout)
-                                + ac_width * PANGO_SCALE);
-
-      cairo_restore (cr);
-
-      if (direction == GTK_TEXT_DIR_RTL)
+      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
         x = 0;
       else
         x = gtk_widget_get_allocated_width (widget) - ac_width;
 
       gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);
 
-      accel_layout = gtk_accel_label_get_accel_layout (accel_label);
       y += get_first_baseline (label_layout) - get_first_baseline (accel_layout) - allocation.y;
 
       gtk_style_context_save_to_node (context, accel_label->priv->accel_node);
@@ -512,11 +488,7 @@ gtk_accel_label_draw (GtkWidget *widget,
 
       g_object_unref (accel_layout);
     }
-  else
-    {
-      GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->draw (widget, cr);
-    }
-  
+
   return FALSE;
 }
 
index 9d13def205bc54c1f00cc9dc0d450daa48486044..a1bbb05fe1d73fe1057b15a32cabc79240c3e16a 100644 (file)
@@ -4299,6 +4299,14 @@ gtk_label_render (GtkCssGadget *gadget,
 
   context = gtk_widget_get_style_context (widget);
 
+  if (GTK_IS_ACCEL_LABEL (widget))
+    {
+      guint ac_width = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
+      width -= ac_width;
+      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
+        x += ac_width;
+    }
+
   if (priv->text && (*priv->text != '\0'))
     {
       lx = ly = 0;